package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.cmd.PouchCMD;
import com.glowdayz.glowmee.dao.BrandDAO;
import com.glowdayz.glowmee.dao.PouchCategoryDAO;
import com.glowdayz.glowmee.dao.PouchDAO;

public class UpdateUserPouchBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public UpdateUserPouchBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int idPouch = Integer.parseInt(request.getParameter("id"));
		
		try {
			request.setAttribute("cmd", PouchDAO.getInstance().getUserPouch(idPouch));
			request.setAttribute("pouchCategory", PouchCategoryDAO.getInstance().getPouchCategoryList(1, -1, "pouchCategoryText"));
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, -1, "brandTitle", null));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/updateUserPouch.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int idPouch = Integer.parseInt(request.getParameter("id"));
		int isDisplay = Integer.parseInt(request.getParameter("isDisplay"));
		String pouchTitle = request.getParameter("pouchTitle");
		int pouchCategory = Integer.parseInt(request.getParameter("pouchCategory"));
		String pouchText = request.getParameter("pouchText");
		
		int[] product = new int[10];
		
		for (int i=0; i<10; i++) {
			String productTemp = request.getParameter("product" + i);
			if (!productTemp.equals("default")) {
				product[i] = Integer.parseInt(request.getParameter("product" + i));
			}
		}
		
		try {
			PouchDAO.getInstance().updateUserPouch(new PouchCMD(idPouch, pouchCategory, pouchTitle, pouchText, isDisplay), product);
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("pouchCategory", PouchCategoryDAO.getInstance().getPouchCategoryList(1, -1, "pouchCategoryText"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("userPouch", PouchDAO.getInstance().getUserPouchList(page, 10, null, null));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"pouch p, register r", "p.idPouch", "p.idRegister=r.idRegister AND r.userType='USER' AND p.isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"pouch p, register r", "p.idPouch", "p.idRegister=r.idRegister AND r.userType='USER' AND p.isDisplay='0'"));
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/listUserPouch.jsp");
		dpr.forward(request, response);
	}

}